function defineInternetWidgets(){
	var FORM_ITEM_ID = 'formId';
	hf.widget.InternetTarifWindow = Ext.extend(hf.widget.TarifWindow, {
		constructor: function(config) {
			hf.widget.InternetTarifWindow.superclass.constructor.apply(this, arguments);
			this.on('select', function(combo, record, index){
				this.changeType(record.data.id);
			});
			this.relayEvents(this.tarifTypeCombo, ['select']);
		},
		
		//override load data method from grid record after window appear
		loadData: function(){
			if(this.record == null){
	    		this.getComponent(FORM_ITEM_ID).getForm().reset();
	    		this.changeType(INTERNET_TARIF_LIMIT);
	    	}else{
	    		this.getComponent(FORM_ITEM_ID).getForm().loadRecord(this.record);
	    		this.changeType(this.record.tarifType);
	    	}	
		},
		
		//disable or eneble form fields depending on tarif type
		changeType: function(type){
			if(type == INTERNET_TARIF_LIMIT){
				this.underSummaField.enable();
				this.overSummaField.enable();
				this.borderValueField.enable();
				this.unlimitSummaField.disable();
			}else if(type == INTERNET_TARIF_UNLIMIT){
				this.underSummaField.disable();
				this.overSummaField.disable();
				this.borderValueField.disable();
				this.unlimitSummaField.enable();
			}
		},
		
		//function is called before saving record
		//validate and set empty fields to 0
		prepareSaving: function(){
			var result = true;
			if(!this.underSummaField.disabled && !this.overSummaField.disabled 
				&& this.overSummaField.getValue() < this.underSummaField.getValue()){
				result = false;
				Ext.Msg.alert(labels['failure.alert.title'], messages['message.tarif.under.greater.over']);
			}
			if(this.underSummaField.getValue() == ''){
				this.underSummaField.setValue(0);
			}
			if(this.overSummaField.getValue() == ''){
				this.overSummaField.setValue(0);
			}
			if(this.borderValueField.getValue() == ''){
				this.borderValueField.setValue(0);
			}
			if(this.unlimitSummaField.getValue() == ''){
				this.unlimitSummaField.setValue(0);
			}
			return result;
		}
	});
	Ext.reg('internet-tarif-window', hf.widget.InternetTarifWindow);
	
	hf.widget.InternetWindow = Ext.extend(hf.widget.CommunalWindow, {
		constructor: function(config) {
			hf.widget.InternetWindow.superclass.constructor.apply(this, arguments);   
		    this.relayEvents(this.currentValueField, ['change']);
		},
		
		//calculate summa when tarif or curenValue  changes
		calculateSumma: function(){
			var summa = 0;
			var currentValue = this.currentValueField.getValue();
			var tarif = this.tarifCombo.getStore().getById(this.tarifCombo.getValue());
			if(tarif != null){
				//for limit internet tarif
				if(tarif.data.tarifType == INTERNET_TARIF_LIMIT){
					if(currentValue <= tarif.data.borderValue){
						summa = delta*tarif.data.underSumma;
					}else{
						summa = tarif.data.borderValue*tarif.data.underSumma + 
						(currentValue - tarif.data.borderValue)*tarif.data.overSumma;
					}
				//for unlimit internet tarif	
				}else if(tarif.data.tarifType == INTERNET_TARIF_UNLIMIT){
					summa = tarif.data.unlimitSumma;
				}
			}
			this.summaField.setValue(summa);
		}
	});
	Ext.reg('internet-window', hf.widget.InternetWindow);
};

function createNetTab(){
	defineInternetWidgets();
	
	//store for tarif records
	var tarifStore = {
		xtype: 'app-store',
		viewAction: 'internetTarif/view.action',
		addAction: 'internetTarif/update.action',
		updateAction: 'internetTarif/update.action',
		deleteAction: 'internetTarif/delete.action',
		columnArray: [
         	{name: indexes['id'], type: INTEGER}, 
         	{name: indexes['enabled'], type: INTEGER},
         	{name: indexes['modelId'],  type: INTEGER},
         	{name: 'tarifName', type: STRING},
         	{name: 'tarifType', type: INTEGER},
         	{name: 'underSumma',  type: FLOAT},
         	{name: 'overSumma',  type: FLOAT},
         	{name: 'borderValue',  type: INTEGER},
         	{name: 'unlimitSumma',  type: FLOAT}
      	],
      	defaultData: {
      		id : FAKE_ID, enabled : ENTITY_STATE_ENABLED, modelId : FAKE_ID, tarifName: '', tarifType: 0, 
      		underSumma: 0.00, overSumma: 0.00, borderValue: 0, unlimitSumma: 0.00
      	}
	};
	
	//store for communal records
	var recordStore = {
		xtype: 'app-store',
		viewAction: 'internet/view.action',
		addAction: 'internet/update.action',
		updateAction: 'internet/update.action',
		deleteAction: 'internet/delete.action',
		columnArray: [
         	{name: indexes['id'], type: INTEGER}, 
         	{name: indexes['enabled'], type: INTEGER},
         	{name: indexes['summa'], type: FLOAT},
         	{name: indexes['date'],  type: DATE},
         	{name: indexes['modelId'],  type: INTEGER},
         	{name: indexes['month'],  type: INTEGER},
         	{name: indexes['year'],  type: INTEGER},
         	{name: 'tarifId',  type: INTEGER},
         	{name: 'monthYear',  type: STRING},
         	{name: 'currentValue', type: INTEGER},
         	{name: 'advancedSumma', type: FLOAT}
      	],
      	defaultData: {
      		id : FAKE_ID, enabled : ENTITY_STATE_ENABLED, modelId : FAKE_ID, tarifId: 0, 
			date: new Date(), year: new Date().getFullYear(), month: new Date().getMonth(), 
			currentValue: 0, advancedSumma: 0.00, summa: 0.00, monthYear: ''}
	};
	
	//window for editing traf record (internet)
	var tarifWindow = {
		xtype: 'internet-tarif-window',
		height:245,
		formItems: [
			{xtype: 'id-hidden-field'},
			{xtype: 'enabled-hidden-field'},
			{xtype: 'modelid-hidden-field'},
			{xtype: 'textfield', fieldLabel: labels['label.tarif.name'], name: 'tarifName', allowBlank: false, blankText: labels['label.value.empty']},
			{xtype: 'tarif-type-combo', ref: '../tarifTypeCombo', dataArray: internetTarifTypes, value: INTERNET_TARIF_LIMIT},
		    {xtype: 'double-number-field', ref: '../underSummaField', fieldLabel: labels['label.internet.undersumma'], name: 'underSumma'},
		    {xtype: 'double-number-field', ref: '../overSummaField', fieldLabel: labels['label.internet.oversumma'], name: 'overSumma'},
		    {xtype: 'integer-number-field', ref: '../borderValueField', fieldLabel: labels['label.internet.bordervalue'],name: 'borderValue'},
		    {xtype: 'double-number-field', ref: '../unlimitSummaField', fieldLabel: labels['label.internet.unlimit.summa'], name: 'unlimitSumma'}
	    ]
	};
	
	//window for editing communal record (internet)
	var communalWindow = {
		xtype: 'internet-window',
		height: 270,
		formItems: [
			{xtype: 'id-hidden-field'},
			{xtype: 'enabled-hidden-field'},
			{xtype: 'modelid-hidden-field'},
			{xtype: 'tarif-combo'},
		    {xtype: 'communal-date-field'},
		    {xtype: 'year-combo'},
		    {xtype: 'month-combo'},
		    {xtype: 'integer-number-field', ref: '../currentValueField', fieldLabel: labels['label.internet.currentvalue'], name: 'currentValue'},
		    {xtype: 'double-number-field', ref: '../summaField', fieldLabel: labels['label.summa'], name: 'summa', value: 0},
		    {xtype: 'double-number-field', ref: '../advancedSummaField', fieldLabel: labels['label.internet.advanced.summa'], name: 'advancedSumma'}
       ]
	};
	
	//grid for displaying tarif data (internet)
	var tarifGrid = {
		xtype: 'app-grid',
		ref: '../../tarifGrid',
		store: tarifStore,
   		colModel: new Ext.grid.ColumnModel({
       		defaults: {width: 70, sortable: true, hideable: true},
        	columns: [new Ext.grid.RowNumberer({header: '№'}),
        			  {xtype: 'gridcolumn', id: 'tarifName', dataIndex: 'tarifName', header: labels['label.tarif.name'], width: 100, tooltip: qtips['qtips.column.tarif.name'], hideable: false},
       			 	  {xtype: 'gridcolumn', id: 'tarifType', dataIndex: 'tarifType', header: labels['label.tarif.type'], tooltip: qtips['qtips.column.tarif.type'],
       			 	  renderer: function(val, x, rec){return internetTarifTypes[val][1];}},
       			 	  {xtype: 'numbercolumn', id: 'underSumma', dataIndex: 'underSumma', header: labels['label.column.internet.undersumma'], tooltip: qtips['qtips.column.internet.undersumma']},
       			 	  {xtype: 'numbercolumn', id: 'overSumma', dataIndex: 'overSumma', header: labels['label.column.internet.oversumma'], tooltip: qtips['qtips.column.internet.oversumma']},
       			 	  {xtype: 'gridcolumn', id: 'borderValue', dataIndex: 'borderValue', header: labels['label.column.internet.bordervalue'], tooltip: qtips['qtips.column.internet.bordervalue']},
       			 	  {xtype: 'numbercolumn', id: 'unlimitSumma', dataIndex: 'unlimitSumma', header: labels['label.column.internet.unlimit.summa'], tooltip: qtips['qtips.column.internet.unlimit.summa']},
       			 	  {xtype: 'action-column'}
       		]
 		}),
 		windowType: tarifWindow,
 		autoExpandColumn: 'tarifName',
 		anchor: '100% 40%'
	};
	
	//grid for displaying communal records (internet)
	var recordGrid = {
		xtype: 'communal-grid',
		title: labels['label.internet.grid.title'],
		ref: '../recordGrid',
		store: recordStore,
   		colModel: new Ext.grid.ColumnModel({
       		defaults: {width: 70, sortable: true, hideable: true},
        	columns: [new Ext.grid.RowNumberer({header: '№'}),
        			  {xtype: 'datecolumn', id: indexes['date'], dataIndex: indexes['date'], header: labels['label.date'], tooltip: qtips['qtips.column.electricity.date']},
       			 	  {xtype: 'gridcolumn', id: indexes['year'], dataIndex: indexes['year'], header: labels['label.year'], tooltip: qtips['qtips.column.year']},
       			 	  {xtype: 'gridcolumn', id: indexes['month'], dataIndex: indexes['month'], header: labels['label.month'], tooltip: qtips['qtips.column.month'],
       			 	  renderer: function(val, x, rec){return months[val][1];}},
       			 	  {xtype: 'gridcolumn', id: 'currentValue', dataIndex: 'сurrentValue', header: labels['label.column.internet.currentvalue'], tooltip: qtips['qtips.column.internet.currentvalue']},
       			 	  {xtype: 'numbercolumn', id: indexes['summa'], dataIndex: indexes['summa'], header: labels['label.summa'], tooltip: labels['label.summa']},
       			 	  {xtype: 'numbercolumn', id: 'advancedSumma', dataIndex: 'advancedSumma', header: labels['label.column.internet.advanced.summa'], tooltip: qtips['qtips.column.internet.advanced.summa']},
       			 	  {xtype: 'action-column'}
       		]
 		}),
 		windowType: communalWindow,
 		autoExpandColumn: indexes['summa'],
 		flex: 1
	};
	
	//tab panel (internet)
	var tab = {
		xtype: 'app-tab',
		title: labels['label.internet.tab.title'],
		items: [{
			layout: {type: 'hbox', align: 'stretch'},
    		anchor: '100%, 100%',
    		items: [recordGrid, {flex: 1, layout: 'anchor', items: [tarifGrid, {xtype: 'chart-panel'}]}]
		}]
	};
	
	return tab;
};